package com.leyou.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.common.constants.LyConstants;
import com.leyou.common.constants.MQConstants;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.pojo.SmsData;
import com.leyou.user.config.PasswordConfig;
import com.leyou.user.mapper.UserMapper;
import entity.AddressDTO;
import entity.User;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserService {
    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    @Autowired
    BCryptPasswordEncoder passwordEncoder;

    public Boolean checkData(String data, Integer type) {
        User user = new User();
        switch (type) {
            case 1:
                user.setUsername(data);
            case 2:
                user.setPhone(data);
        }
        QueryWrapper<User> query = Wrappers.query(user);

        return userMapper.selectCount(query) == 0;
    }

    public void sendVerifyCode(String phone) {
        String code = RandomStringUtils.randomNumeric(6);
        redisTemplate.opsForValue().set(LyConstants.REDIS_KEY_PRE + phone, code, 5, TimeUnit.MINUTES);
        System.out.println("验证码" + code);
        SmsData smsData = new SmsData();
        smsData.setPhone(phone);
        smsData.setCode(code);
        //   rocketMQTemplate.convertAndSend(MQConstants.Topic.SMS_TOPIC_NAME,smsData);

    }

    public void register(User user, String code) {
        //1.校验验证码是否存在和正确
        String rediscode = redisTemplate.opsForValue().get(LyConstants.REDIS_KEY_PRE + user.getPhone());
        if (rediscode == null || !rediscode.equals(code)) {
            throw new LyException(ExceptionEnum.INVALID_VERIFY_CODE);
        }

        //2.对密码进行加盐加密
        try {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            //3.保存用户数据
            userMapper.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public User query(String username, String password) {
        User user = new User();

        user.setUsername(username);

        QueryWrapper<User> query = Wrappers.query(user);
        User loginUser = userMapper.selectOne(query);
        if (loginUser == null) {
            throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }
        if (!passwordEncoder.matches(password, loginUser.getPassword())) {
            throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }
        return loginUser;
    }

    public AddressDTO findAddressId(Long userId, Long id) {
        AddressDTO address = new AddressDTO();
        address.setId(1L);
        address.setStreet("珠吉路58号津安创业园一层黑马程序员");
        address.setCity("广州");
        address.setDistrict("天河区");
        address.setAddressee("小飞飞");
        address.setPhone("15800000000");
        address.setProvince("广东");
        address.setPostcode("510000");
        address.setIsDefault(true);
        return address;
    }
}
